const http = require("http");
const fs=require("fs");
const mysql=require("mysql");
const ejs=require("ejs");
const conn=mysql.createConnection({
    host:"127.0.0.1",
    user:"root",
    password:"root",
    database:"stusys"
})
conn.connect((err)=>{
    if(err)throw err;
});
const server = http.createServer((req, res) => {
    res.setHeader("content-type", "text/html;charset=utf8");
    req.path = req.url.split("?")[0];
    switch (req.path) {
        case "/":
            //res.end("登录页面")
            res.end(fs.readFileSync("./views/index.html"))
            break;
        case "/login":
         //检验登录
         let postData="";
         req.on("data",(chunk)=>{
            postData+=chunk.toString();
         });
         req.on("end",()=>{
            console.log(postData);
            let myurl="http://localhost:8080/?"+postData;
            let myURL=new URL(myurl);
            let username=myURL.searchParams.get("username");
            let password=myURL.searchParams.get("password");
            let sql="select * from admin where adminname=? and adminpwd=?";
            conn.query(sql,[username,password],(err,data)=>{
                if(err) throw err;
                if(data.length>0){
                    let user={
                        username:username,
                        status:0//登陆成功
                    }
                    res.end(JSON.stringify(user))
                }else{
                    let user={
                        username:username,
                        status:1,//登陆成功
                        msg:"用户名或密码错误"
                    }
                    res.end(JSON.stringify(user))
                }                
            })
        
         });
           break;
        case "/main":
            //res.end("主页面页面")
            res.end(fs.readFileSync("./views/main.html"));
            break;
        case "/list":
            let sql="select * from stu";
            conn.query(sql,(err,data)=>{
                console.log(data);
                ejs.renderFile("./views/list.html", { stus:data }, (err, data) => {
                    if (err) throw err;
                    res.end(data);
            })
        })
            //res.end(fs.readFileSync("./views/list.html"));
            break;
        case "/add":
            let url="http://localhost:8080/"+req.url;
            let myURL=new URL(url);
            let stuname=myURL.searchParams.get("stuname");
            let stuage=myURL.searchParams.get("stuage");
            let stusex=myURL.searchParams.get("stusex");
            let mobile=myURL.searchParams.get("mobile");
            let address=myURL.searchParams.get("address");

            let insertSql="insert into stu(stuname,stuage,stusex,mobile,address) values(?,?,?,?,?)"
            conn.query(insertSql,[stuname,stuage,stusex,mobile,address],(err,data)=>{
                if(err) throw err;
                res.end("<h1>添加成功!<a href='/list'>返回列表</a></h1>")
            })
            
            break;
        case "/toAdd":

            res.end(fs.readFileSync("./views/add.html"));
                break;
        case "/upd":
            res.end("修改学生");
            break;
        case "/delById":
            let url02="http://localhost:8080/"+req.url;
            let myURL02=new URL(url02);
            let id=myURL02.searchParams.get("id");
            console.log(id);
            
            let mysql="delete from stu where stuid=?";
            conn.query(mysql,[id],(err,data)=>{
                if(err) throw err;
                res.end("<h1>删除成功!<a href='/list'>返回列表</a></h1>")
            })
           
            break;
        case "/findById":
            let url03="http://localhost:8080/"+req.url;
            let myURL03=new URL(url03);
            let id3=myURL03.searchParams.get("id");
            console.log(id3);
            let fsql="select * from stu where stuid=?";
            conn.query(fsql,id3,(err,data)=>{
                ejs.renderFile("./views/show.html", { stu:data[0] }, (err, data) => {
                    if (err) throw err;
                    res.end(data);
            })

            })
            break;
        default:
            res.end("页面不存在");
            break;
    }
})
server.listen(8080, () => {
    console.log("服务器运行在8080端口....");
});